import json


def highlight_template(title, data):
    papers = []

    for k, i in enumerate(data):
        aList = []
        for au in i['authors']:
            aList.append({
                "type": "textRun",
                "textRun": {
                    "text": au['name'],
                    "style": {
                        "link": {
                            "url": au['url']
                        }
                    }
                }
            })
            aList.append({
                "type": "textRun",
                "textRun": {
                    "text": ", ",
                    "style": {}
                }
            })

        cList = []
        for ca in i['category']:
            cList.append({
                "type": "textRun",
                "textRun": {
                    "text": ca,
                    "style": {},
                    "location": {
                        "zoneId": "0",
                        "startIndex": 199,
                        "endIndex": 231
                    }
                }
            })
        cList[0]['textRun']['style'] = {"bold": True}

        comment = []
        if 'comments' in i:
            comment = [{
                "type": "paragraph",
                "paragraph": {
                    "elements": [
                        {
                            "type": "textRun",
                            "textRun": {
                                "text": i['comments'],
                                "style": {},
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 170,
                                    "endIndex": 188
                                }
                            }
                        }
                    ],
                    "location": {
                        "zoneId": "0",
                        "startIndex": 170,
                        "endIndex": 188
                    },
                    "lineId": "3CkqFR"
                }
            }]

        summary = {
            "type": "paragraph",
            "paragraph": {
                "elements": [],
                "location": {
                    "zoneId": "0",
                    "startIndex": 265,
                    "endIndex": 1338
                },
                "lineId": "X4c2Zt"
            }
        }
        if 'summary' in i:
            if 'shighlight' in i:
                start = 0
                for key, v in sorted(i['shighlight'].items()):
                    if start < key:
                        summary['paragraph']['elements'].append({
                            "type": "textRun",
                            "textRun": {
                                "text": i['summary'][start:key],
                                "style": {
                                    "location": {
                                        "zoneId": "0",
                                        "startIndex": 74,
                                        "endIndex": 75
                                    }
                                }
                            }
                        })
                    if start < v:
                        summary['paragraph']['elements'].append({
                            "type": "textRun",
                            "textRun": {
                                "text": i['summary'][max(start, key):v],
                                "style": {
                                    "backColor": {
                                        "red": 255,
                                        "green": 246,
                                        "blue": 122,
                                        "alpha": 0.8
                                    },
                                    "location": {
                                        "zoneId": "0",
                                        "startIndex": 74,
                                        "endIndex": 75
                                    }
                                }
                            }    
                        })
                    start = max(start, v)
                if start < len(i['summary']):
                    summary['paragraph']['elements'].append({
                        "type": "textRun",
                        "textRun": {
                            "text": i['summary'][start:],
                            "style": {
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 74,
                                    "endIndex": 75
                                }
                            }
                        }
                    })
            else:
                summary['paragraph']['elements'].append({
                    "type": "textRun",
                    "textRun": {
                        "text": i['summary'],
                        "style": {
                            "location": {
                                "zoneId": "0",
                                "startIndex": 74,
                                "endIndex": 75
                            }
                        }
                    }        
                })

        atitle = []
        if 'thighlight' in i and len(i['thighlight']):
            start = 0
            for key, v in sorted(i['thighlight'].items()):
                if start < key:
                    atitle.append({
                        "type": "textRun",
                        "textRun": {
                            "text": i['title'][start:key],
                            "style": {
                                "bold": True,
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 74,
                                    "endIndex": 75
                                }
                            }
                        }
                    })
                if start < v:
                    atitle.append({
                        "type": "textRun",
                        "textRun": {
                            "text": i['title'][max(start, key):v],
                            "style": {
                                "backColor": {
                                    "red": 255,
                                    "green": 246,
                                    "blue": 122,
                                    "alpha": 0.8
                                },
                                "bold": True,
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 74,
                                    "endIndex": 75
                                }
                            }
                        }    
                    })
                start = max(start, v)
            if start < len(i['title']):
                atitle.append({
                    "type": "textRun",
                    "textRun": {
                        "text": i['title'][start:],
                        "style": {
                            "bold": True,
                            "location": {
                                "zoneId": "0",
                                "startIndex": 74,
                                "endIndex": 75
                            }
                        }
                    }
                })
        else:
            atitle.append({
                "type": "textRun",
                "textRun": {
                    "text": i['title'],
                    "style": {
                        "bold": True
                    },
                    "location": {
                        "zoneId": "0",
                        "startIndex": 74,
                        "endIndex": 75
                    }
                }        
            })

        papers.extend([
            {
                "type": "paragraph",
                "paragraph": {
                    "elements": [
                        {
                            "type": "textRun",
                            "textRun": {
                                "text": " ",
                                "style": {},
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 17,
                                    "endIndex": 18
                                }
                            }
                        },
                        *atitle,
                        {
                            "type": "textRun",
                            "textRun": {
                                "text": " ",
                                "style": {
                                    "bold": True
                                },
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 74,
                                    "endIndex": 75
                                }
                            }
                        }
                    ],
                    "style": {
                        "headingLevel": 6,
                        "list": {
                            "type": "number",
                            "indentLevel": 1,
                            "number": (k+1)
                        },
                        "collapse": True
                    },
                    "location": {
                        "zoneId": "0",
                        "startIndex": 16,
                        "endIndex": 75
                    },
                    "lineId": "D8qBU2"
                }
            },
            {
                "type": "paragraph",
                "paragraph": {
                    "elements": aList,
                    "location": {
                        "zoneId": "0",
                        "startIndex": 76,
                        "endIndex": 139
                    },
                    "lineId": "epBG4u"
                }
            },
            {
                "type": "paragraph",
                "paragraph": {
                    "elements": [
                        {
                            "type": "textRun",
                            "textRun": {
                                "text": f"arXiv:{i['rawId']}",
                                "style": {
                                    "bold": True,
                                    "link": {
                                        "url": f"https://export.arxiv.org/abs/{i['rawId']}"
                                    }
                                },
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 140,
                                    "endIndex": 156
                                }
                            }
                        },
                        {
                            "type": "textRun",
                            "textRun": {
                                "text": " [",
                                "style": {},
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 156,
                                    "endIndex": 158
                                }
                            }
                        },
                        {
                            "type": "textRun",
                            "textRun": {
                                "text": "pdf",
                                "style": {
                                    "bold": True,
                                    "link": {
                                        "url": f"https://export.arxiv.org/pdf/{i['rawId']}"
                                    }
                                },
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 158,
                                    "endIndex": 161
                                }
                            }
                        },
                        {
                            "type": "textRun",
                            "textRun": {
                                "text": ", ",
                                "style": {},
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 161,
                                    "endIndex": 163
                                }
                            }
                        },
                        {
                            "type": "textRun",
                            "textRun": {
                                "text": "other",
                                "style": {
                                    "bold": True,
                                    "link": {
                                        "url": f"https://export.arxiv.org/format/{i['rawId']}"
                                    }
                                },
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 163,
                                    "endIndex": 168
                                }
                            }
                        },
                        {
                            "type": "textRun",
                            "textRun": {
                                "text": "]",
                                "style": {},
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 168,
                                    "endIndex": 169
                                }
                            }
                        }
                    ],
                    "location": {
                        "zoneId": "0",
                        "startIndex": 140,
                        "endIndex": 169
                    },
                    "lineId": "PWwIT0"
                }
            },
            *comment,
            {
                "type": "paragraph",
                "paragraph": {
                    "elements": [
                        {
                            "type": "textRun",
                            "textRun": {
                                "text": "Subjects: ",
                                "style": {},
                                "location": {
                                    "zoneId": "0",
                                    "startIndex": 189,
                                    "endIndex": 199
                                }
                            }
                        },
                        *cList
                    ],
                    "location": {
                        "zoneId": "0",
                        "startIndex": 189,
                        "endIndex": 264
                    },
                    "lineId": "fYSbdd"
                }
            },
            summary,
            {
                "type": "paragraph",
                "paragraph": {
                    "elements": [],
                    "location": {
                        "zoneId": "0",
                        "startIndex": 1339,
                        "endIndex": 1339
                    },
                    "lineId": "FhnEVp"
                }
            }
        ])

    papers.extend([
        {
            "type": "paragraph",
            "paragraph": {
                "elements": [
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": "",
                            "style": {},
                            "location": {
                                "zoneId": "0",
                                "startIndex": 1154,
                                "endIndex": 1198
                            }
                        }
                    }
                ],
                "style": {
                    "headingLevel": 6
                },
                "location": {
                    "zoneId": "0",
                    "startIndex": 1153,
                    "endIndex": 1198
                },
                "lineId": "mxtVA0"
            }
        },
        {
            "type": "paragraph",
            "paragraph": {
                "elements": [
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": "论文来源：https://export.arxiv.org/list/cs.CL/new",
                            "style": {},
                            "location": {
                                "zoneId": "0",
                                "startIndex": 1154,
                                "endIndex": 1198
                            }
                        }
                    }
                ],
                "style": {
                    "headingLevel": 6
                },
                "location": {
                    "zoneId": "0",
                    "startIndex": 1153,
                    "endIndex": 1198
                },
                "lineId": "mxtVA0"
            }
        }       
    ])
    
    return {
        "FolderToken": "",
        "Content": json.dumps({
            "title": {
                "elements": [
                    {
                        "type": "textRun",
                        "textRun": {
                            "text": title,
                            "style": {},
                            "location": {
                                "zoneId": "0",
                                "startIndex": 0,
                                "endIndex": 8
                            }
                        }
                    }
                ],
                "location": {
                    "zoneId": "0",
                    "startIndex": 0,
                    "endIndex": 8
                },
                "lineId": "XxEzN0"
            },
            "body": {
                "blocks": papers
            }
        })
    }

